package winOrDie.graphics.maps;

import org.apache.xerces.dom.DOMImplementationImpl;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import winOrDie.Util.XML_Manager;
import winOrDie.graphics.maps.Map_Board_Editor.XML_ELEMENTS_Map_Board;

public class WoD_Map_Editor 
{
	
	public static class XML_ELEMENTS_WoD_Map
	{
		public static final String Element_WoD_Map = "WoD_Map";
		public static final String Attribute_MapTitle = "MapTitle"; 
	}

	/**
	 * This public static method returns a {@link Element} object with the data of {@link WoD_Map} object passed as parameter.
	 * The {@link Element} is created from {@link Document} object passed as parameter 
	 * @param woDMap
	 * @param document
	 * @return {@link Element}
	 * @throws Exception
	 */
	public static Element get_Element_From_WoD_Map(
			WoD_Map woDMap,
			Document document) 
	throws Exception
	{
		Element element_WoD_Map = document.createElement(XML_ELEMENTS_WoD_Map.Element_WoD_Map);
		element_WoD_Map.setAttribute(XML_ELEMENTS_WoD_Map.Attribute_MapTitle, woDMap.getMapTitle());
		
		element_WoD_Map.appendChild(
				woDMap.getMapBoard().get_XML_Element(document)
			);
		
		return element_WoD_Map;
	}
	
	/**
	 * This public static method loads data of {@link Element} passed as parameter into returned {@link WoD_Map} object.
	 * @param element
	 * @return {@link WoD_Map}
	 * @throws Exception
	 */
	public static WoD_Map get_WoD_Map_From_Element(Element element) throws Exception
	{
		if (!element.getNodeName().equals(XML_ELEMENTS_WoD_Map.Element_WoD_Map))
		{
			Exception exception = new Exception("The element is not a WoD_Map element.");
			throw exception;
		}
		
		/*
		 * Load and check map title
		 */
		String mapTitle = element.getAttribute(XML_ELEMENTS_WoD_Map.Attribute_MapTitle);
		if (mapTitle.length() < 1)
		{
			Exception exception = new Exception("The map title property is empty.");
			throw exception;
		}
		
		
		/*
		 * Load and check Map_Board
		 */
		NodeList nodeList_Map_Board = element.getElementsByTagName(XML_ELEMENTS_Map_Board.Element_Map_Board);
		if (nodeList_Map_Board.getLength() < 1)
		{
			Exception exception = new Exception("There is no Map_Board element");
			throw exception;
		}
		
		Element element_Map_Board = (Element) nodeList_Map_Board.item(0);
		Map_Board mapBoard = new Map_Board();
		try {
			mapBoard = Map_Board_Editor.get_Map_Board_From_Element(element_Map_Board);
		} catch (Exception e) {
			Exception exception = new Exception("Error loading Map_Board data. " + e.getMessage());
			throw exception;
		}
		
		
		/*
		 * Set the data to WoD_Map object
		 */
		WoD_Map woDMap = new WoD_Map();
		woDMap.setMapTitle(mapTitle);
		woDMap.setMapBoard(mapBoard);
		return woDMap;
	}
	
	/**
	 * This public static method save {@link WoD_Map} data into file.
	 * @param woDMap {@link WoD_Map} Object that contans data to save
	 * @param fileName The file name
	 * @throws Exception
	 */
	public static void save_WoD_Map_To_File(WoD_Map woDMap, String fileName) throws Exception
	{
		DOMImplementation domImplementation = new DOMImplementationImpl();
		Document document = domImplementation.createDocument("Wod_Map", "Wod_Map", null);
		
		document.replaceChild(
				woDMap.get_XML_Element(document), 
				document.getFirstChild());
		
		XML_Manager xmlManager = new XML_Manager();
		xmlManager.saveXMLElementToFile(document.getDocumentElement(), fileName);
		
	}
	
	/**
	 * This public static method returns a {@link WoD_Map} object from data loaded inside file
	 * @param fileName The file name
	 * @return {@link WoD_Map}
	 * @throws Exception
	 */
	public static WoD_Map get_WoD_Map_From_File(String fileName) throws Exception
	{
		XML_Manager xmlManager = new XML_Manager();
		Document document = xmlManager.Load_File_into_XML_Document(fileName);
		return get_WoD_Map_From_Element(document.getDocumentElement());
	}
}
